Reg Expression help

Hello,

I was wondering what would be the best reg expression to allow me to switch the last "," for every line within the string to a "&". I just can't seem to get my expression correct. Any help would be appreciated.

Thank you,
Jim
 

string source = "Source:  775460-1-804-01  Requirement: 339\n" \\-
        "Source:  775460-1-804-02  Requirement: 47, 150, 182, 214\n" \\-
                "Source:  775460-1-804-03  Requirement: 81, 82, 83"
                                
 
string result= "Source:  775460-1-804-01  Requirement: 339\n" \\-
                "Source:  775460-1-804-02  Requirement: 47, 150, 182 & 214\n" \\-
                "Source:  775460-1-804-03  Requirement: 81, 82 & 83"

SystemAdmin - Thu Nov 01 20:29:47 EDT 2012

Re: Reg Expression help
Mathias Mamsch - Fri Nov 02 06:18:02 EDT 2012

The dot (.) in regexp will not match on newline, therefore I guess the following regexp will do what you want. Note that you might want to replace the comma by " &". Then you would need to build up another buffer. And somehow you got your end of line comments messed up? Use / instead of backslash.
 

string source = "Source:  775460-1-804-01  Requirement: 339\n" //-
        "Source:  775460-1-804-02  Requirement: 47, 150, 182, 214\n" //-
                "Source:  775460-1-804-03  Requirement: 81, 82, 83"
                                
 
string result= "Source:  775460-1-804-01  Requirement: 339\n" //-
                "Source:  775460-1-804-02  Requirement: 47, 150, 182& 214\n" //-
                "Source:  775460-1-804-03  Requirement: 81, 82& 83"
 
Regexp regCommaReplace = regexp2 ".*(,)[^,\n]*(\n|$)"
Buffer bufCommaReplace = create() 
 
string replaceComma (string s) {
   bufCommaReplace = s 
   int pos = 0
   while (search(regCommaReplace , bufCommaReplace , pos)) {
      int commaPos = pos + start 1 
      set(bufCommaReplace , commaPos, '&')
      pos += end 0
   }
   return stringOf bufCommaReplace 
}
 
string sResult = replaceComma source
print (sResult == result) "\n"
print sResult "\n"

 


Hope this helps, regards, Mathias

 

 

 


Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS